#include "qwy_calculate.h"
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <QDebug>
#include <QString>
#include <QStringList>

// 构造函数
qwy_calculate::qwy_calculate() {
    // 初始化代码（如果有）
}

// 析构函数
qwy_calculate::~qwy_calculate() {
    // 清理代码（如果有）
}

// 实现 qwy_add 方法
double qwy_calculate::qwy_add(double last, double now) {
    return last + now;
}

// 实现 qwy_totla_add 方法
double qwy_calculate::qwy_totla_add(QString str)
{
    double total = 0;
    QStringList numberList = str.split(" ", Qt::SkipEmptyParts); // 使用空格分割字符串 s

    foreach (const QString &str, numberList) {
        bool ok;
        double number = str.toDouble(&ok);
        if (ok) {
            total += number;
        } else {
            return 0;
        }
    }

    return total;
}

// 实现 qwy_proportion 方法
double qwy_calculate::qwy_proportion(double parts, double whole) {
    if (whole == 0) {
        std::cerr << "Error: Division by zero in qwy_proportion." << std::endl;
        return 0; // 防止除以零
    }
    return parts / whole *100;
}

// 实现 qwy_growth 方法
double qwy_calculate::qwy_growth(double last, double now) {
    if (last == 0) {
        std::cerr << "Error: Division by zero in qwy_growth." << std::endl;
        return 0; // 防止除以零
    }

    double diff = now - last ;

    double ret = diff / last  ;

    return ret*100;
}


